package com.di.base.service.Impl;

import com.di.base.mapper.StorageMapper;
import com.di.base.service.StorageService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Service
public class StorageServiceImpl implements StorageService {
    @Resource
    StorageMapper storageMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void decreaseStock(String productId, int count) {
        log.info("库存服务当前XID: {}", RootContext.getXID());
        log.info("开始扣减库存");
        int updated = storageMapper.decrease(productId, count);
        if (updated <= 0) {
            throw new RuntimeException("库存不足");
        }
        log.info("库存扣减完成");
    }
}
